<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="minimum-scale=1.0, width=device-width, maximum-scale=1.0, user-scalable=no"/>
    <meta charset="utf-8">
    <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
    <title>Higher Order Components</title>
</head>
<body>
<div id="react-container"></div>
  <script type="text/babel">

    const { Component } = React
    const { render } = ReactDOM

    const DataComponent = (ComposedComponent, url) =>
      class DataComponent extends Component {

          constructor(props) {
              super(props)
              this.state = {
                  data: [],
                  loading: false,
                  loaded: false
              }
          }

          componentWillMount() {
              this.setState({loading:true})
              fetch(url)
                  .then(response => response.json())
                  .then(data => this.setState({
                      loaded: true,
                      loading: false,
                      data
                  }))
          }

          render() {
              return (
                  <div className="data-component">
                      {(this.state.loading) ?
                          <div>Loading...</div> :
                          <ComposedComponent {...this.state}
                                             {...this.props} />}
                  </div>
              )
          }
      }

    const PeopleList = ({data}) =>
      <ol className="people-list">
          {data.results.map((person, i) => {
              const {first, last} = person.name
              return <li key={i}>{first} {last}</li>
          })}
      </ol>

    const RandomMeUsers = DataComponent(
                            PeopleList,
                            "https://randomuser.me/api?results=10"
                        )

    render(
      <RandomMeUsers />,
      document.getElementById('react-container')
    )


  </script>
</body>
</html>
